﻿
namespace Common
{
    using System.Configuration;
    using System.Web;
    using System.Web.Configuration;
    using System.Windows.Forms;
    /// <summary>
    /// Summary description for ConfigurationOperator
    /// </summary>
    public class ConfigurationOperator
    {
        public enum ConfigType
        {
            /// <summary> 
            /// asp.net网站的config文件 
            /// </summary> 
            WebConfig = 1,
            /// <summary> 
            /// Windows应用程序的config文件 
            /// </summary> 
            ExeConfig = 2
        }

        private Configuration config;
        private string configPath;
        private ConfigType configType;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configType"></param>
        public ConfigurationOperator(ConfigType configType)
        {
            this.configType = configType;
            if (configType == ConfigType.ExeConfig)
            {
                configPath = Application.ExecutablePath;
            }
            else
            {
                configPath = HttpContext.Current.Request.ApplicationPath;
            }
            Initialize();
        }

        public Configuration Configuration
        {
            get { return config; }
            set { config = value; }
        }

        /// <summary> 
        /// 构造函数 
        /// </summary> 
        /// <param name="path">.config文件的位置</param> 
        /// <param name="type">.config文件的类型，只能是网站配置文件或者应用程序配置文件</param> 
        public ConfigurationOperator(string configPath, ConfigType configType)
        {
            this.configPath = configPath;
            this.configType = configType;
            Initialize();
        }
        /// <summary>
        /// 实例化configuration,根据配置文件类型的不同，分别采取了不同的实例化方法 
        /// </summary>
        private void Initialize()
        {
            //如果是WinForm应用程序的配置文件 
            if (configType == ConfigType.ExeConfig)
            {
                config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configPath);
            }
            else//WebForm的配置文件 
            {
                config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(configPath);
            }
        }
        /// <summary>
        /// 添加应用程序配置节点，如果已经存在此节点，则会修改该节点的值 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddAppSetting(string key, string value)
        {
            AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
            if (appSetting.Settings[key] == null)//如果不存在此节点，则添加 
            {
                appSetting.Settings.Add(key, value);
            }
            else//如果存在此节点，则修改 
            {
                ModifyAppSetting(key, value);
            }
        }
        /// <summary>
        /// 添加数据库连接字符串节点，如果已经存在此节点，则会修改该节点的值 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="connectionString"></param>
        public void AddConnectionString(string key, string connectionString)
        {
            ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
            if (connectionSetting.ConnectionStrings[key] == null)//如果不存在此节点，则添加 
            {
                ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString);
                connectionSetting.ConnectionStrings.Add(connectionStringSettings);
            }
            else//如果存在此节点，则修改 
            {
                ModifyConnectionString(key, connectionString);
            }
        }

        /// <summary>
        /// 添加数据库连接字符串节点，如果已经存在此节点，则会修改该节点的值 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="duration"></param>
        public void AddoutputCacheProfiles(string name, int duration)
        {
            ///此处可发散为：修改其余的节点也是类似于此处的操作
            OutputCacheSettingsSection OutputCacheSettings = (OutputCacheSettingsSection)config.GetSection("system.web/caching/outputCacheSettings");
            if (OutputCacheSettings.OutputCacheProfiles[name] == null)//如果不存在此节点，则添加 
            {
                OutputCacheProfile OutputCacheProfiles = new OutputCacheProfile(name);
                OutputCacheProfiles.Duration = duration;
                OutputCacheProfiles.Location = System.Web.UI.OutputCacheLocation.Any;
                OutputCacheProfiles.VaryByParam = "*";
                //此处暂时只是修改了duration一个参数，故方法仅一个参数，如果要修改所有的参数，请自行扩充
                OutputCacheSettings.OutputCacheProfiles.Add(OutputCacheProfiles);
            }
            else//如果存在此节点，则修改 
            {
                ModifyoutputCacheProfiles(name, duration);
            }
        }


        /// <summary>
        /// 修改应用程序配置节点，如果不存在此节点，则会添加此节点及对应的值 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="newduration"></param>
        public void ModifyoutputCacheProfiles(string name, int newduration)
        {
            OutputCacheSettingsSection OutputCacheSettings = (OutputCacheSettingsSection)config.GetSection("system.web/caching/outputCacheSettings");
            if (OutputCacheSettings.OutputCacheProfiles[name] != null)//如果不存在此节点，则添加 
            {
                OutputCacheSettings.OutputCacheProfiles[name].Duration = newduration;
            }
            else//如果不存在此节点，则添加
            {
                AddoutputCacheProfiles(name, newduration);
            }
        }

        /// <summary> 
        /// 修改应用程序配置节点，如果不存在此节点，则会添加此节点及对应的值 
        /// </summary> 
        /// <param name="key">节点名称</param> 
        /// <param name="value">节点值</param> 
        public void ModifyAppSetting(string key, string newValue)
        {
            AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
            if (appSetting.Settings[key] != null)//如果存在此节点，则修改 
            {
                appSetting.Settings[key].Value = newValue;
            }
            else//如果不存在此节点，则添加 
            {
                AddAppSetting(key, newValue);
            }
        }
        /// <summary> 
        /// 修改数据库连接字符串节点，如果不存在此节点，则会添加此节点及对应的值 
        /// </summary> 
        /// <param name="key">节点名称</param> 
        /// <param name="value">节点值</param> 
        public void ModifyConnectionString(string key, string connectionString)
        {
            ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
            if (connectionSetting.ConnectionStrings[key] != null)//如果存在此节点，则修改 
            {
                connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
            }
            else//如果不存在此节点，则添加 
            {
                AddConnectionString(key, connectionString);
            }
        }
        /// <summary> 
        /// 保存所作的修改 
        /// </summary> 
        public void Save()
        {
            config.Save();
        }
    }
}
